<?php

  $name = "phpackage.phar";
  Phar::mapPhar($name);

  $tmpPath = tempnam(sys_get_temp_dir(), "PHPackager");
  unlink($tmpPath);
  mkdir($tmpPath);

  $pharObject = new Phar($name);
  $pharObject->extractTo($tmpPath);

  $packages = glob($tmpPath . "/*", GLOB_ONLYDIR);

  $paths = array();
  foreach ($packages as $path) {
    array_push($paths, $path . "/library");
  }

  array_push($paths, dirname(__FILE__) . DIRECTORY_SEPARATOR . "library");
  array_push($paths, get_include_path());

  set_include_path(implode(PATH_SEPARATOR, $paths));

  require_once "JooS/Config.php";
  require_once "JooS/PHPackager/Manager.php";

  if (!JooS_Config::PHPackager()->path()) {
    JooS_Config::PHPackager()->path = dirname(__FILE__);
  }

  JooS_PHPackager_Manager::addPackages($packages);

  $fileSPLObjects =  new RecursiveIteratorIterator( new RecursiveDirectoryIterator($tmpPath), RecursiveIteratorIterator::CHILD_FIRST);
  foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) {
    if ($fileSPLObject->isFile())
      @unlink($fullFileName);
    elseif ($fileSPLObject->isDir())
      @rmdir($fullFileName);
    else
      exit;
  }
  @rmdir($tmpPath);
